package com.moleq.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import com.moleq.db.DatabaseHelper;
import com.moleq.util.ImageUtil;

public class ItemPicService {

	private DatabaseHelper DBHelper;

	public ItemPicService(Context shareContext) {
		this.DBHelper = new DatabaseHelper(shareContext);
	}

	public Bitmap getImageByUPC(String upc) {

		Bitmap result = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery(
				"SELECT ip_image FROM t_item_pic WHERE F01 = '" + upc + "'",
				null);
		if (cursor.moveToFirst()) {
			byte[] bytes = cursor.getBlob(0);
			result = ImageUtil.btyes2Bitmap(bytes);
		}
		cursor.close();
		return result;

	}
	
	public Bitmap getImageSmByUPC(String upc) {

		Bitmap result = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery(
				"SELECT ip_image_sm FROM t_item_pic WHERE F01 = '" + upc + "'",
				null);
		if (cursor.moveToFirst()) {
			byte[] bytes = cursor.getBlob(0);
			result = ImageUtil.btyes2Bitmap(bytes);
		}
		cursor.close();
		return result;

	}

	public Bitmap getSmallImageByUPC(String upc) {

		Bitmap result = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery(
				"SELECT ip_image_sm FROM t_item_pic WHERE F01 = '" + upc + "'",
				null);
		if (cursor.moveToFirst()) {
			byte[] bytes = cursor.getBlob(0);
			result = ImageUtil.btyes2Bitmap(bytes);
		} else {
			// result = getDefaultLogo();
		}

		cursor.close();
		return result;

	}

	public Drawable getImageByID(String id) {

		return ImageUtil.bitmapToDrawable(getSmallImageByUPC(id));

	}

	public Bitmap getImage(String id, int width, int height) {
		return ImageUtil.zoomBitmap(getImageByUPC(id), width, height);
	}

	public int ImageUpdate(String upc, byte[] b) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ip_image", b);
		String[] args = { String.valueOf(upc) };
		return db.update("t_item_pic", values, "F01=?", args);
	}

	public boolean checkIsExistPic(String upc) {
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_item_pic where F01 = '"
				+ upc + "'", null);
		if (cursor.moveToFirst()) {
			result = true;
		}
		cursor.close();
		return result;
	}

	public long ImageInsert(String upc, byte[] b) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("F01", upc);
		values.put("ip_image", b);
		long rowid = db.insert("t_item_pic", null, values);
		return rowid;
	}

	public boolean ImageDelete(String upc) {
		try {
			SQLiteDatabase db = DBHelper.getWritableDatabase();
			db.execSQL("Delete from t_item_pic where F01 = '" + upc + "'");
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		return true;
	}
	
	public long ImageInsert(String upc,byte[] b,byte[] b2){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("F01", upc);
		values.put("ip_image", b);
		values.put("ip_image_sm", b2);
		long rowid = db.insert("t_item_pic", null, values);
		return rowid;
	}
	
	public int ImageUpdate(String upc,byte[] b,byte[] b2){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ip_image", b);
		values.put("ip_image_sm", b2);
		String[] args = {String.valueOf(upc)};
		return db.update("t_item_pic", values, "F01=?", args);
	}

}
